package com.wd.amodel.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wd.amodel.R;
import com.wd.amodel.adapter.ImageAdapter;
import com.wd.amodel.listener.ImageClickListener;
import com.wd.amodel.util.ColorConstant;
import com.wd.amodel.util.EmotionModelUtils;
import com.wd.amodel.vm.ListViewModel;
import com.wd.amodel.vm.listener.ILoginImageListListener;
import com.wd.base.service.ServiceFactory;
import com.wd.base.ui.BaseFragment;
import com.wd.base.util.Logger;
import com.wd.base.util.ViewUtils;
import com.wd.common.userservice.IUserCommonService;
import com.wd.common.userservice.bean.TestBean;
import com.wd.common.userservice.bean.UserBean;
import com.wd.common.userservice.listener.UserInfoListener;

import java.util.ArrayList;
import java.util.List;

//import androidx.lifecycle.ViewModelProviders;

/**
 * NA<BR>
 *
 * @author w4425
 * @version [V1.0.0, 2021/4/21]
 * @since V1.0.0
 */
public class ListFragment extends BaseFragment {
    private TextView tvUser;
    private TextView tvMember;
    private RecyclerView recyclerView;
    private ListViewModel viewModel;
    private ImageAdapter mImageAdapter;
    private List<String> mListString = new ArrayList<>();

    /**
     * 获取MainFragment实例对象
     *
     * @return MainFragment
     */
    public static ListFragment newInstance() {
        return new ListFragment();
    }

    @Override
    protected String getLogTag() {
        return "MainFragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initView(View view) {
        recyclerView = ViewUtils.findViewById(view, R.id.recycler_view);
        tvUser = ViewUtils.findViewById(view, R.id.tv_user);
        tvMember = ViewUtils.findViewById(view, R.id.tv_member);
        // 设置布局方式
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        if (mImageAdapter == null) {
            mImageAdapter = new ImageAdapter(getActivity(), mListString);
            mImageAdapter.setListener(new ImageClickListener() {
                @Override
                public void onClick(int position, String name) {
                    Logger.i("onClick name = " + name);

                }
            });
        }
        recyclerView.setAdapter(mImageAdapter);

        String color = EmotionModelUtils.getColorValue(ColorConstant.ORDER_VALUE_ONE);
        Logger.d("color = " + color);
        tvUser.setTextColor(Color.parseColor(color));
    }

    @Override
    protected void initViewModel() {
//        viewModel = ViewModelProviders.of(getActivity()).get(ListViewModel.class);
        observer();
    }

    private void observer() {
        viewModel.observeMemberData(this, new Observer<TestBean>() {
            @Override
            public void onChanged(TestBean t) {
                Logger.i("TestBean.msg = " + t.msg);
                tvMember.setText("member:" + t.msg);
            }
        });
    }

    @Override
    protected void loadData() {
        viewModel.loadListImage(new ILoginImageListListener() {
            @Override
            public void imageList(List<String> list) {
                mListString.addAll(list);
                mImageAdapter.notifyDataSetChanged();
            }
        });
        viewModel.postMemberDataFromNet();

        ServiceFactory.getService(IUserCommonService.class).queryUserInfo("", new UserInfoListener() {
            @Override
            public void UserInfo(UserBean bean) {
                tvUser.setText("测试 - user:" + bean.name);
            }
        });

    }

}